package com.example.util;

import com.example.util.model.MobileCard;
import com.example.util.mq.disruptor.FileReqData;
import com.example.util.task.TemWhiteMobilesCastAsync;
import com.example.util.util.FileFolder;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONException;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@Slf4j
public class TempWhiteListUtil {


    /**
     * 挑选手机号
     *
     * @param fileName
     * @param fileWriterDir
     * @throws JSONException
     */
    public static void chooseMobile(String fileName, String fileWriterDir, String terminalManageUrl) {

        //1.读取手机号存储文件
        List<String> collect = FileFolder.readFileAsList(fileName);

        TemWhiteMobilesCastAsync.batchProcessTempWhiteList(collect, fileName, fileWriterDir, terminalManageUrl, null);
    }

    /**
     * 挑选手机号
     *
     * @param fileName
     * @param param
     * @throws JSONException
     */
    public static void chooseMobile(String fileName, FileReqData param) {

        //1.读取前八位手机号存储文件
        List<String> list1 = FileFolder.readFileAsList(fileName);
        //分批次梳理
        List<String> list = MobileCard.getDatasByCardType(param.getCardType());
        List<String> allList = new ArrayList<>();
        List<String> collect = null;
        AtomicReference<String> mobile = new AtomicReference<>();
        for (String number : list) {
            collect = list1.stream().map(item -> {
                mobile.set(number + item);
                return mobile.get();
            }).collect(Collectors.toList());
            allList.addAll(collect);
        }
//        List<String> collect = datas.values().stream().map(item -> {
//            return MobileCard.A.getNumber() + item;
//        }).collect(Collectors.toList());
        Collections.shuffle(allList);
        log.info("allList 的size:{}", allList.size());
        TemWhiteMobilesCastAsync.batchProcessTempWhiteList(allList, fileName, param.getWriteDir(), param.getTerminalManageUrl(), param.getToken());
    }


    /**
     * 写文件，把手机号后八位写出文件到指定目录
     *
     * @throws JSONException
     */
    public static void writeMobileEight(Integer i, String writeDir) {
        //把手机号后八位写出文件到指定目录
        TemWhiteMobilesCastAsync.batchProcessMobileNextEight(i, writeDir);
    }

}
